package com.ttdevs.es.service;

import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.ttdevs.es.R;
import com.ttdevs.util.BaseClass;

public class MainAlertWindowManager extends BaseClass implements OnClickListener {

	private Context context;

	private WindowManager windowManager;
	private WindowManager.LayoutParams params;
	private View alertView;

	private Button btTopLeft, btTopRight, btBottomLeft, btBottomRight, btCenter;

	public MainAlertWindowManager(Context context, Handler handler) {
		super(context, handler);
		this.context = context;
		this.windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		initAlertView(context);
	}

	private void initAlertView(Context context) {
		params = new WindowManager.LayoutParams();
		params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
		// params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;// 可以�?
		// params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;//不能�?
		// params.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;//不能�?
		params.width = WindowManager.LayoutParams.MATCH_PARENT;
		params.height = WindowManager.LayoutParams.MATCH_PARENT;
		params.gravity = Gravity.CENTER;
		params.alpha = 0f;// 透明度，0全透 ，1不透

		alertView = LayoutInflater.from(context).inflate(R.layout.service_unlock, null);

		btTopLeft = (Button) alertView.findViewById(R.id.btTopLeft);
		btTopRight = (Button) alertView.findViewById(R.id.btTopRight);
		btBottomLeft = (Button) alertView.findViewById(R.id.btBottomLeft);
		btBottomRight = (Button) alertView.findViewById(R.id.btBottomRight);
		btCenter = (Button) alertView.findViewById(R.id.btCenter);
		btTopLeft.setOnClickListener(this);
		btTopRight.setOnClickListener(this);
		btBottomLeft.setOnClickListener(this);
		btBottomRight.setOnClickListener(this);
		btCenter.setOnClickListener(this);
	}

	public void addView() {
		windowManager.addView(alertView, params);
	}

	public void removeView() {
		windowManager.removeView(alertView);
	}

	// 解锁事件
	int[] key = new int[4];
	int count = 0;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btTopLeft:
			key[count] = 1;
			break;
		case R.id.btTopRight:
			key[count] = 3;
			break;
		case R.id.btBottomLeft:
			key[count] = 4;
			break;
		case R.id.btBottomRight:
			key[count] = 2;
			break;
		case R.id.btCenter:
			// key[count] = 5;
			break;

		default:
			break;
		}
		count++;
		if (count == 4) {
			if (checkKey(key)) {
				sendMessage(STATE_UNLOCK);
			} else {
				Toast.makeText(context, "X", Toast.LENGTH_SHORT).show();
				for (int i = 0; i < key.length; i++) {
					key[i] = 0;
				}
			}
			count = 0;
		}
	}

	private boolean checkKey(int[] key) {
		if (key[0] == 1 && key[1] == 2 && key[2] == 3 && key[3] == 4) {
			return true;
		}
		return false;
	}
}
